﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.BLL;
using System.IO;
using System.Drawing;
namespace OF.Web
{
    /// <summary>
    /// 客户端访问接口页面
    /// </summary>
    public partial class api : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string method = WebUtils.GetPara("method", false);
                string areaid = WebUtils.GetPara("areaid", false);
                string schoolid = WebUtils.GetPara("schoolid", false);
                string school = WebUtils.GetPara("school", false);
                string buyerid = WebUtils.GetPara("buyerid", false);
                string shoptype = WebUtils.GetPara("shoptype", false);

                string loginname = WebUtils.GetPara("loginname", false);
                string pwd = WebUtils.GetPara("pwd", false);
                string phone = WebUtils.GetPara("phone", false);
                string address = WebUtils.GetPara("address", false);

                string sellerid = WebUtils.GetPara("sellerid", false);
                string catid = WebUtils.GetPara("catid", false);
                string dishid = WebUtils.GetPara("dishid", false);

                string pageid = WebUtils.GetPara("pageid", false);
                string state = WebUtils.GetPara("state", false);
                string usertype = WebUtils.GetPara("usertype", false);
                string result = string.Empty;

                string orderid = WebUtils.GetPara("orderid", false);
                string photo = WebUtils.GetPara("photo", false);

                BLL.Addr.Area bll_area = new BLL.Addr.Area();
                BLL.Dish.Dish bll_dish = new BLL.Dish.Dish();
                BLL.Order.Order bll_order = new BLL.Order.Order();
                BLL.User.User bll_user = new BLL.User.User();
                BLL.Base.Base bll_base = new BLL.Base.Base();
                WebUtils util = new WebUtils();
                switch (method)
                {
                    #region 区域模块
                    //获取区域(参数:areaid)
                    case "addr.arealist.get":
                        result = bll_area.doGetAreaList(areaid);
                        util.PrintResult(result, false);
                        break;
                    //根据区域获取学校(参数:areaid)
                    case "addr.school.get":
                        result = bll_area.doGetAreaSchoolList(areaid);
                        util.PrintResult(result, false);
                        break;
                    //根据学校或是区域查询商家(参数:schoolid，areaid)
                    case "user.sellerbyschool.get":
                        string cityid = WebUtils.GetPara("cityid", false);
                        string sort = WebUtils.GetPara("sort", false);
                        result = bll_area.doGetSellerList(schoolid, areaid, cityid,shoptype,sort, pageid);
                        util.PrintResult(result, false);
                        break;
                    //模糊搜索学校(参数:school)
                    case "addr.school.search":
                        result = bll_area.doGetSchoolList(school);
                        util.PrintResult(result, false);
                        break;
                    //保存用户地址
                    case "addr.useraddr.add":
                        result = bll_area.doSendAddress(address, buyerid);
                        util.PrintResult(result, false);
                        break;
                    //修改用户地址
                    case "addr.useraddr.modify":
                        string addressid = WebUtils.GetPara("addressid", false);
                        result = bll_area.doModifyAddress(address, state, addressid);
                        util.PrintResult(result, false);
                        break;
                    //删除用户地址
                    case "addr.useraddr.delete":
                        addressid = WebUtils.GetPara("addressid", false);
                        result = bll_area.doDeleteAddress(addressid);
                        util.PrintResult(result, false);
                        break;
                    //根据用户查询地址列表(参数:buyerid)
                    case "addr.useraddrlist.get":
                        result = bll_area.doGetUserAddress(buyerid);
                        util.PrintResult(result, false);
                        break;
                    //获取区域和区域下的学校(参数:areaid)
                    case "addr.areaschoollist.get":
                        result = bll_area.getAreaSchoollist(areaid);
                        util.PrintResult(result, false);
                        break;
                    #endregion

                    #region 用户模块
                    //买家注册
                    case "user.reg":
                        result = bll_user.doSendUser(loginname, pwd, address);
                        util.PrintResult(result, false);
                        break;
                    //买家登录
                    case "user.login":
                        result = bll_user.doValidLogin(loginname, pwd);
                        util.PrintResult(result, false);
                        break;
                    //用户留言添加       
                    case "user.feedback.add":
                        string contact = WebUtils.GetPara("contact", false);               //留言联系人
                        string message = WebUtils.GetPara("message", false);       //留言内容 
                        result = bll_user.doSendFeedBackInfo(usertype, contact, message);
                        util.PrintResult(result, false);
                        break;
                    //卖家登录
                    case "user.seller.login":
                        result = bll_user.LoginSeller(loginname, pwd);
                        util.PrintResult(result, false);
                        break;
                    //获取商家列表
                    case "user.sellerlist.get":
                        result = bll_user.doGetAllSellerList();
                        util.PrintResult(result, false);
                        break;
                    //根据商家类型筛选商家
                    case "user.sellerbytype.get":
                       
                        result = bll_user.doGetSellerListByType(shoptype);
                        util.PrintResult(result, false);
                        break;
                    //根据商家id获取商户信息
                    case "user.sellerbyid.get":
                        result = bll_user.getSellerById(sellerid);
                        util.PrintResult(result, false);
                        break;
                    //修改商户信息
                    case "user.seller.modify":
                        string arrivtime = WebUtils.GetPara("arrivtime", false);
                        string minprice = WebUtils.GetPara("minprice", false);
                        string sendneedprice = WebUtils.GetPara("sendneedprice", false);
                        string average_spend = WebUtils.GetPara("average_spend", false);
                        result = bll_user.modifySeller(sellerid, arrivtime, minprice, phone, sendneedprice, average_spend);
                        util.PrintResult(result, false);
                        break;
                    //修改商户当前状态
                    case "user.sellerstate.modify":
                        result = bll_user.modifySellerState(sellerid);
                        util.PrintResult(result, false);
                        break;
                    //模糊搜索商户(参数:school)
                    case "user.seller.search":
                        string seller = WebUtils.GetPara("seller", false);
                        result = bll_user.doGetSellerList(seller);
                        util.PrintResult(result, false);
                        break;
                    //获取商户类型
                    case "user.sellertype.get":
                        result = bll_user.getShopType();
                        util.PrintResult(result, false);
                        break;
                    #endregion

                    #region 菜品模块
                    //根据商家获取菜类和菜
                    case "dish.catdish.get":
                        result = bll_dish.doGetCatDish(sellerid);
                        util.PrintResult(result, false);
                        break;
                    //根据商家获取类别
                    case "dish.catsbyseller.get":
                        result = bll_dish.doGetCatsBySeller(sellerid);
                        util.PrintResult(result, false);
                        break;
                    //根据类别获取菜品
                    case "dish.dishlistbycat.get":
                        result = bll_dish.doGetDishByCat(catid);
                        util.PrintResult(result, false);
                        break;
                    //根据菜品获取菜品详细
                    case "dish.dishdetail.get":
                        result = bll_dish.doGetDishDetail(dishid);
                        util.PrintResult(result, false);
                        break;
                    //添加菜品
                    case "dish.addupdate":
                        string dishname = WebUtils.GetPara("dishname", false);
                        string price = WebUtils.GetPara("price", false);
                        string _type = WebUtils.GetPara("type", false);
                        result = bll_dish.addupdateDish(dishname, price, catid, photo, sellerid, state, dishid, _type);
                        util.PrintResult(result, false);
                        break;
                    //根据商家获取所有菜
                    case "dish.alldishbyseller.get":
                        result = bll_dish.getAllDishbySeller(sellerid, state);
                        util.PrintResult(result, false);
                        break;
                    //通过菜品id修改状态
                    case "dish.DishState.modify":
                        result = bll_dish.modifyDishState(dishid);
                        util.PrintResult(result, false);
                        break;
                    #endregion

                    #region 订单模块
                    //订餐提交接口
                    case "order.add":
                        string dishs = WebUtils.GetPara("dishs", false);
                        string scheduledtime = WebUtils.GetPara("scheduledtime", false);
                        string sendaddress = WebUtils.GetPara("sendaddress", false);
                        string population = WebUtils.GetPara("population", false);
                        string remark = WebUtils.GetPara("remark", false);
                        string type = WebUtils.GetPara("type", false);
                        result = bll_order.doAddOrder(sellerid, buyerid, dishs, scheduledtime, phone, sendaddress, population, remark, type, state);
                        util.PrintResult(result, false);
                        break;
                    //修改待提交的订单信息
                    case "order.update.info":
                        dishs = WebUtils.GetPara("dishs", false);
                        scheduledtime = WebUtils.GetPara("scheduledtime", false);
                        sendaddress = WebUtils.GetPara("sendaddress", false);
                        population = WebUtils.GetPara("population", false);
                        remark = WebUtils.GetPara("remark", false);
                        type = WebUtils.GetPara("type", false);
                        result = bll_order.doUpdateOrder(orderid, sellerid, dishs, scheduledtime, sendaddress, population, remark, type);
                        util.PrintResult(result, false);
                        break;
                    //根据用户查询订单列表
                    case "order.orderlist.get":
                        result = bll_order.doGetOrderList(buyerid, pageid, state);
                        util.PrintResult(result, false);
                        break;
                    //修改订单状态
                    case "order.update":
                        string modify = WebUtils.GetPara("modify", false);
                        result = bll_order.UpdateOrder(orderid, modify, usertype);
                        util.PrintResult(result, false);
                        break;
                    //根据用户查询订单列表
                    case "order.sellerorder.get":
                        string userid = WebUtils.GetPara("userid", false);
                        string ordertype = WebUtils.GetPara("ordertype", false);
                        state = WebUtils.GetPara("state", false);
                        string operate = WebUtils.GetPara("operate", false);
                        string id = WebUtils.GetPara("id", false);
                        result = bll_order.doGetOrderList(userid, usertype, ordertype, state, operate, id);
                        util.PrintResult(result, false);
                        break;
                    //根据订单id查询订单明细
                    case "order.sellerorderinfo.get":
                        result = bll_order.doGetSellerOrderInfo(orderid);
                        util.PrintResult(result, false);
                        break;
                    //添加订单评论
                    case "order.comment.add":
                        string eat_score = WebUtils.GetPara("eat_score", false);
                        string serve_score = WebUtils.GetPara("serve_score", false);
                        string details = WebUtils.GetPara("details", false);
                        result = bll_order.addOrderComment(buyerid, orderid, eat_score, serve_score, details);
                        util.PrintResult(result, false);
                        break;
                    #endregion

                    #region 系统模块

                    //获取系统版本号
                    case "system.version.get":
                        result = bll_base.GetSystemVersion();
                        util.PrintResult(result, false);
                        break;
                    #endregion

                    #region 上传图片
                    //上传图片
                    case "photo.upload":
                        string filename = WebUtils.GetPara("filename", false);   //图片名+后缀
                        string filetype = WebUtils.GetPara("filetype", false);   //图片类型 ：如image/png
                        string imageBuffer = WebUtils.GetPara("imageBuffer", false);
                        result = OF.Common.Utils.UploadPhoto(filename, filetype, imageBuffer);  //图片64位编码字符串
                        util.PrintResult(result, false);
                        break;
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Common.LogHelper.WriteLog(ex.ToString() + "\n" + ex.StackTrace, "API页面错误");
            }
        }
    }
}